<template>
  <div class="room-type">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>房型管理</span>
          <el-button type="primary" @click="handleAdd">新增房型</el-button>
        </div>
      </template>
      
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="typeName" label="房型名称" />
        <el-table-column prop="bedType" label="床型" />
        <el-table-column prop="maxOccupancy" label="最大入住人数" width="120" />
        <el-table-column prop="roomSize" label="面积(㎡)" width="100" />
        <el-table-column prop="price" label="价格(元)" width="100" />
        <el-table-column prop="roomCount" label="房间数" width="80" />
        <el-table-column prop="availableCount" label="可用数" width="80" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'info'">
              {{ row.status === 1 ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑房型' : '新增房型'" width="600px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="房型名称" prop="typeName">
          <el-input v-model="form.typeName" />
        </el-form-item>
        <el-form-item label="床型" prop="bedType">
          <el-input v-model="form.bedType" />
        </el-form-item>
        <el-form-item label="最大入住人数" prop="maxOccupancy">
          <el-input-number v-model="form.maxOccupancy" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="房间面积" prop="roomSize">
          <el-input v-model="form.roomSize">
            <template #append>平方米</template>
          </el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price">
            <template #append>元/晚</template>
          </el-input>
        </el-form-item>
        <el-form-item label="房型描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="设施" prop="amenities">
          <el-input v-model="form.amenities" type="textarea" :rows="2" 
            placeholder='请输入JSON格式，如：["无线网络","空调","电视"]' />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { getRoomTypeList, createRoomType, updateRoomType, deleteRoomType } from '@/api/room'

const loading = ref(false)
const tableData = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const formRef = ref<FormInstance>()

const form = reactive({
  id: null,
  typeName: '',
  bedType: '',
  maxOccupancy: 2,
  roomSize: '',
  price: '',
  description: '',
  amenities: '',
  status: 1
})

const rules: FormRules = {
  typeName: [
    { required: true, message: '请输入房型名称', trigger: 'blur' }
  ],
  bedType: [
    { required: true, message: '请输入床型', trigger: 'blur' }
  ],
  maxOccupancy: [
    { required: true, message: '请输入最大入住人数', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入价格', trigger: 'blur' }
  ]
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getRoomTypeList()
    tableData.value = res.data
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  isEdit.value = false
  Object.assign(form, {
    id: null,
    typeName: '',
    bedType: '',
    maxOccupancy: 2,
    roomSize: '',
    price: '',
    description: '',
    amenities: '',
    status: 1
  })
  dialogVisible.value = true
}

const handleEdit = (row: any) => {
  isEdit.value = true
  Object.assign(form, row)
  dialogVisible.value = true
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (isEdit.value) {
          await updateRoomType(form)
          ElMessage.success('更新成功')
        } else {
          await createRoomType(form)
          ElMessage.success('创建成功')
        }
        dialogVisible.value = false
        loadData()
      } catch (error) {
        console.error(error)
      }
    }
  })
}

const handleDelete = async (row: any) => {
  await ElMessageBox.confirm(
    `确定要删除房型"${row.typeName}"吗？删除前请确保没有房间使用该房型。`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
  
  try {
    await deleteRoomType(row.id)
    ElMessage.success('删除成功')
    loadData()
  } catch (error) {
    console.error(error)
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.room-type {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>